﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ASMS.Excel2DbTool.Utils
{
    /// <summary>
    /// 网络中华机构详细信息
    /// </summary>
    public class wlzh_jigouxiangxixinxi
    {
        private static int[] s_typeCount = new int[]{
        0,3,
        3,3,
        6,2,
        8,7,
        15,2
        };

        private static string[] s_cateNames = new string[]{
            "航空货代",
            "陆运业务",
            "汽运业务",
            "水运业务",
            "专业物流"
        };
        private static string[] s_typeNames = new string[]{
            "报关",
            "航空代理",
            "快件",
            "报关的副本",
            "多式联运",
            "铁路代理",
            "公路代理",
            "汽车运输",
            "报关的副本",
            "船泊承运",
            "船务代理",
            "集装箱场站",
            "码头",
            "水路代理",
            "修箱",
            "仓储",
            "专业物流"
        };
        public static void Run(string filePath, string destFileName)
        {
            DataTable dataTable = Sunboxsoft.Excel.ReadTool.ReadExcel(filePath, true);

            StringBuilder contentBuilder = new StringBuilder();

            for (int index = 0; index < dataTable.Rows.Count; index++)
            {
                string nameString = Convert.ToString(dataTable.Rows[index][0]).Trim();
                string geoString = Convert.ToString(dataTable.Rows[index][1]).Trim();

                #region 检查
                if (string.IsNullOrEmpty(nameString) || string.Equals(nameString, "无"))
                {
                    continue;
                }

                if (string.IsNullOrEmpty(geoString))
                {
                    continue;
                }

                string[] geoArr = geoString.Split(new char[] { ',' });
                if (geoArr.Length != 2)
                {
                    continue;
                }
                double geoLng = 0;
                double geoLat = 0;

                if (!double.TryParse(geoArr[0], out geoLng))
                {
                    continue;
                }
                if (!double.TryParse(geoArr[1], out geoLat))
                {
                    continue;
                }
                #endregion

                int cate = new Random().Next(0, 4);
                string cateName = s_cateNames[cate];
                Thread.Sleep(10);
                int type = s_typeCount[cate * 2] + new Random().Next(0, s_typeCount[cate * 2 + 1] - 1);
                string typeName = s_typeNames[type];
                Thread.Sleep(10);

                Console.WriteLine(string.Format("{0}\t\t{1}", cateName, typeName));
                if (index == 0)
                {
                    contentBuilder.AppendFormat("{{\"name\":\"{0}\",\"geo\":[{1},{2}],\"cate\":{3},\"type\":{4},\"cateName\":\"{5}\",\"typeName\":\"{6}\"}}", nameString, geoLng, geoLat, cate, type, cateName, typeName);
                }
                else
                {
                    contentBuilder.AppendFormat(",{{\"name\":\"{0}\",\"geo\":[{1},{2}],\"cate\":{3},\"type\":{4},\"cateName\":\"{5}\",\"typeName\":\"{6}\"}}", nameString, geoLng, geoLat, cate, type, cateName, typeName);
                }
            }
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat("[{0}]", contentBuilder);

            string destFilePath = string.Format("{0}\\{1}", AppDomain.CurrentDomain.BaseDirectory, destFileName);
            System.IO.File.WriteAllText(destFilePath, builder.ToString());
            Console.WriteLine(string.Format("转换完毕{0},count:{1}", destFilePath, dataTable.Rows.Count));
        }
    }
}